/***************************************************************************************
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
*
* NEMU is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#include <common.h>
#include <device/map.h>
#include <SDL2/SDL.h>
#include <unistd.h>

#define AUDIO_REG(reg) *(audio_base+reg)
#define AUDIO_REG_OFFSET(reg) (sizeof(uint32_t)*reg)
enum {
  reg_freq,
  reg_channels,
  reg_samples,
  reg_sbuf_size,
  reg_init,
  reg_count,
  reg_length,
  nr_reg
};

static uint8_t *sbuf = NULL;
static uint32_t *audio_base = NULL;
static int rfd = -1, wfd = -1;
static volatile int count = 0;
static void audio_play(void *userdata, uint8_t *stream, int len) {
  int nread = len;
  if (count < len) nread = count;
  int b = 0;
  while (b < nread) {
    int n = read(rfd, stream, nread);
    if (n > 0) b += n;
  }

  AUDIO_REG(reg_count) = count = count - nread;
  if (len > nread) {
    memset(stream + nread, 0, len - nread);
  }
}
static void audio_write(uint8_t *buf, int len) {
  int nwrite = 0;
  while (nwrite < len) {
    int n = write(wfd, buf, len);
    if (n == -1) n = 0;
    AUDIO_REG(reg_count) = count = count + n;
    nwrite += n;
  }
}
static void audio_io_handler(uint32_t offset, int len, bool is_write) {
  int ret;
  if(!is_write){
    switch (offset)
    {
    case AUDIO_REG_OFFSET(reg_init):
      int fds[2];
      ret = pipe(fds);
      assert(ret == 0);
      rfd = fds[0],wfd = fds[1];
      break;
    }
  }
  else{
    switch (offset)
    {
    case AUDIO_REG_OFFSET(reg_samples):
      SDL_AudioSpec s = {
        .freq=AUDIO_REG(reg_freq),
        .format=AUDIO_S16SYS,
        .channels=AUDIO_REG(reg_channels),
        .samples=AUDIO_REG(reg_samples),
        .callback = audio_play,
        .userdata = NULL
      };
      AUDIO_REG(reg_count)=count=0;
      ret = SDL_InitSubSystem(SDL_INIT_AUDIO);
      if (ret == 0) {
        SDL_OpenAudio(&s, NULL);
        SDL_PauseAudio(0);
      }
      break;
      case AUDIO_REG_OFFSET(reg_length):
        audio_write(sbuf,AUDIO_REG(reg_length));
      break;
    }
  }
}

void init_audio() {
  uint32_t space_size = sizeof(uint32_t) * nr_reg;
  audio_base = (uint32_t *)new_space(space_size);
#ifdef CONFIG_HAS_PORT_IO
  add_pio_map ("audio", CONFIG_AUDIO_CTL_PORT, audio_base, space_size, audio_io_handler);
#else
  add_mmio_map("audio", CONFIG_AUDIO_CTL_MMIO, audio_base, space_size, audio_io_handler);
#endif

  sbuf = (uint8_t *)new_space(CONFIG_SB_SIZE);
  add_mmio_map("audio-sbuf", CONFIG_SB_ADDR, sbuf, CONFIG_SB_SIZE, NULL);
  AUDIO_REG(reg_sbuf_size)=CONFIG_SB_SIZE;
}
